/*
 * Copyright (c) 2024 Huawei Technologies Co.,Ltd.
 *
 * openGauss is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 * http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTIIMCULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * ---------------------------------------------------------------------------------------
 *
 * imcu.h
 * routines to support ColStore
 *
 *
 * IDENTIFICATION
 * src/include/access/imcs/imcu.h
 *
 * ---------------------------------------------------------------------------------------
 */

#ifndef IMCU_H
#define IMCU_H

#include "vecexecutor/vectorbatch.h"
#include "imcs.h"
#include "imcs_mem_alloc.h"
#include "utils/datum.h"
#include "storage/lock/lwlock.h"
#include "storage/cu.h"
#include <map>
#ifdef ENABLE_HTAP
#include "storage/htap/htap_delete_map.h"
#endif


struct IMCUDesc : public CUDesc {
    /*
     * the pointer to the IMCU
     */
    void *m_cu;

    /*
     * IMCUDesc status
     */
    int flag;

public:
    IMCUDesc();
    ~IMCUDesc();

    void Reset();
    void Destroy() {}
};

struct IMCUDesc_Tag {
    int32 cu_id;
    int attnum;
};

#ifdef ENABLE_HTAP
#define SEGMENT_TREE_LEAF_NUM 0x1000

struct SegmentTree {
    /*
     * segment tree
     * segment_tree_range: The numeric range of the segment tree, i.e. the number of segment leaf subnodes.
     */
    uint32 **data_array;
    uint32 array_size;
    uint32 logic_leaf_num;
    uint32 *cuid_cache;
    int cache_used;
    uint64 *left_border;
    uint64 *right_border;
    uint64 max_ctid;
    uint32 right_ptr;
};
#endif

struct IMCSDesc {
    Oid imcs_oid;
    uint32 max_cu_id;
    uint32 rowgroup_num;
#ifdef ENABLE_HTAP
    IMCSDesc(){};
    ~IMCSDesc();
    htap::HTAPDeleteMap **deletedList;
#endif
    CommitSeqNo last_flushed_csn;
    HTAB *im_cudesc_table;

    /*
     * lock for imcs alter
     */
    LWLock *imcs_desc_lock;
    slock_t imcs_rg_lock;
    std::map<int, pthread_rwlock_t *> rg_lock_map;

    /*
     * latch for imcu operation
     */
    LWLock *imcu_desc_latch;

    uint32 cu_num_in_mem;
    uint64 cu_mem_size;
    uint32 cu_num_in_disk;
    uint64 cu_disk_size;

#ifdef ENABLE_HTAP
    SegmentTree *segment_tree;

    int markDeleted(uint32 rg_idx, uint32 row_idx);
    bool isDeleted(uint32 rg_idx, uint32 row_idx);
    int increaseRGNum();
#endif
private:
};

/* IMCU struct:
 * before compressing
 *
 * +------------------------+  <--  m_srcBuf               -
 * |                        |                              |
 * |       Header Info      |                         m_srcBufSize
 * |                        |                              |
 * +------------------------+  <--  m_nulls      -         |
 * |                        |                    |         |
 * |       Null Bitmap      |            m_bpNullRawSize   |
 * |                        |                    |         |
 * +------------------------+  <--  m_srcData    -         |
 * |                        |                    |         |
 * |     Compressed Data    |               m_srcData_size  |
 * |                        |                    |         |
 * +------------------------+                    -         |
 * |      Padding Data      |                              |
 * +------------------------+                              -
 */
typedef CU IMCU;

#endif
